package com.wd.health.view.activity.home;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.wd.health.R;
import com.wd.health.view.activity.patient.PublishActivity;
import com.wd.health.view.fragment.CircleFragment;
import com.wd.health.view.fragment.HomeFragment;
import com.wd.health.view.fragment.VideoFragment;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {

    private ViewPager vp;
    private RadioGroup rg;
    private RadioButton b1;
    private RadioButton b2;
    private RadioButton b3;
    private List<Fragment> flist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ImmersionBar.with(this).init();
        initView();
        initData();
    }

    private void initData() {
        flist = new ArrayList<>();
        flist.add(new HomeFragment());
        flist.add(new CircleFragment());
        flist.add(new VideoFragment());
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return flist.get(position);
            }

            @Override
            public int getCount() {
                return flist.size();
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                rg.check(rg.getChildAt(position).getId());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.b1:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.b2:
                        vp.setCurrentItem(1);
//                        if (vp.getCurrentItem()==1){
//                            b2.setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    Intent intent = new Intent(HomeActivity.this, PublishActivity.class);
//                                    startActivity(intent);
//                                }
//                            });
//                        }
                        break;
                    case R.id.b3:
                        vp.setCurrentItem(2);
                        break;
                }
            }
        });

    }

    private void initView() {
        vp = findViewById(R.id.vp_home);
        rg = findViewById(R.id.rg_home);
        b1 = findViewById(R.id.b1);
        b2 = findViewById(R.id.b2);
        b3 = findViewById(R.id.b3);
    }
}